package com.cpr.rwqd_sjjn.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8081","http://127.0.0.1:8081") // 允许的来源
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("Authorization", "Content-Type")
                .allowedHeaders("*")
                .allowCredentials(true);
    }
}

//package com.pc_hzd.config;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.CorsRegistry;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//
//@Configuration
//public class WebConfig implements WebMvcConfigurer {
//
//    @Autowired
//    private JwtInterceptor jwtInterceptor;
//
//    /**
//     * 配置跨域支持
//     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**") // 匹配所有路径
//                .allowedOrigins("http://localhost:8081", "http://127.0.0.1:8081") // 允许的来源
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的 HTTP 方法
//                .allowedHeaders("*") // 允许的请求头
//                .allowCredentials(true); // 允许发送 Cookie
//    }
//
//    /**
//     * 配置拦截器
//     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor)
//                .addPathPatterns("/hzd/**") // 拦截所有 /hzd/ 下的接口
//                .excludePathPatterns(
//                        "/hzd/sendVerificationCode",//获取验证码
//                        "/hzd/register",
//                        "/hzd/change-password",//忘记密码
//                        "/hzd/login-by-number-password", // 放行登录接口
//                        "/hzd/login-by-email-code"       // 放行验证码登录接口
//                );
//    }
//}